<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="打折券编码" prop="discountCode">
        <el-input v-model="dataForm.discountCode" placeholder="打折码"></el-input>
      </el-form-item>
      <el-form-item label="折扣券名称" prop="couponName">
        <el-input v-model="dataForm.couponName" placeholder="折扣券名称"></el-input>
      </el-form-item>
      <el-form-item label="折扣(0-1)" prop="discount">
        <el-input v-model="dataForm.discount" placeholder="折扣(0-1)"></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="status">
        <el-radio v-model="dataForm.status" label=1>启用</el-radio>
        <el-radio v-model="dataForm.status" lable=0>停用</el-radio>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/admin/sysempdiscountcoupon'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          discountCode: '',
          couponName: '',
          discount: '',
          status: ''
        },
        dataRule: {
          discountCode: [{
            required: true,
            message: '打折码不能为空',
            trigger: 'blur'
          }],
          couponName: [{
            required: true,
            message: '折扣券名称不能为空',
            trigger: 'blur'
          }],
          discount: [{
            required: true,
            message: '折扣(0-1)不能为空',
            trigger: 'blur'
          }],
          status: [{
            required: true,
            message: '是否启用(1-启用,0-停用)不能为空',
            trigger: 'blur'
          }],
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            } else {
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            }
          }
        })
      }
    }
  }
</script>
